package com.logic.landseaserver.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.logic.common.exception.BusinessException;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.SEO;
import com.logic.landseaserver.persistence.read.SEOReadMapper;
import com.logic.landseaserver.persistence.write.SEOWriteMapper;
import com.logic.landseaserver.service.ISEOService;
import com.logic.landseaserver.ws.dto.SEODTO;


/**
 * @Author: jack
 * @Date: 2017/5/4
 * @Description: SEO接口实现类
 */
@Service
public class SEOServiceImpl implements ISEOService {

	@Autowired
	private SEOReadMapper seoReadMapper;
	
	@Autowired
	private SEOWriteMapper seoWriteMapper;
	
	
	@Override
	public void delete(String arg0) throws BusinessException {
		// TODO Auto-generated method stub

	}

	@Override
	public SEODTO get(Integer arg0) throws BusinessException {

		return seoReadMapper.getSEODetail(arg0);
	}

	@Override
	public int insertSEO(SEODTO seoDTO) throws BusinessException {
		int activityInt = -1;
		if (validateForInsert(seoDTO)) 
		{//需补充验证
			SEO seo = new SEO(); 
			int orderId;
			setSEOBySEODTO(seo,seoDTO);
			Date date=new Date();
//			seo.setCreatedDtm(new Date());
			seo.setCreatedBy(seoDTO.getUserId());
			seo.setCreatedDtm(date);
			seo.setLastModifiedBy(seoDTO.getUserId());
			seo.setLastModifiedDtm(date);
//			order.setOrderFromPage(orderFromPage);
			seoWriteMapper.insertSelective(seo);
		}
		return activityInt;
	}

	@Override
	public List<SEODTO> query(QueryUtil queryUtil) {
		List<SEODTO> result = seoReadMapper.getSEODTOList(queryUtil);
		return result;
	}

	@Override
	public void update(Object obj) throws BusinessException {
			
	}

	@Override
	public Boolean validate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validateForDelete(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean validateForInsert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return Boolean.valueOf(true);
	}

	@Override
	public Boolean validateForUpdate(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return null;
	}


	//通过orderDTO创建order
	public void setSEOBySEODTO(SEO seo ,SEODTO SEODTO){
		if(!StringUtils.isEmpty(SEODTO.getProId()))
		{
			seo.setProId(SEODTO.getProId());
		}
		if(!StringUtils.isEmpty(SEODTO.getTitle()))
		{
			seo.setTitle(SEODTO.getTitle());
		}
		if(!StringUtils.isEmpty(SEODTO.getDescription()))
		{
			seo.setDescription(SEODTO.getDescription());
		}
		if(!StringUtils.isEmpty(SEODTO.getCreatedDtm()))
		{
			seo.setCreatedDtm(SEODTO.getCreatedDtm());
		}
		if(!StringUtils.isEmpty(SEODTO.getCreatedBy()))
		{
			seo.setCreatedBy(SEODTO.getCreatedBy());
		}
		if(!StringUtils.isEmpty(SEODTO.getLastModifiedDtm()))
		{
			seo.setLastModifiedDtm(SEODTO.getLastModifiedDtm());
		}
		if(!StringUtils.isEmpty(SEODTO.getLastModifiedBy()))
		{
			seo.setLastModifiedBy(SEODTO.getLastModifiedBy());
		}
		if(!StringUtils.isEmpty(SEODTO.getVersion()))
		{
			seo.setVersion(SEODTO.getVersion());
		}
		if(!StringUtils.isEmpty(SEODTO.getDeleted()))
		{
			seo.setDeleted(SEODTO.getDeleted());
		}
	}

	@Override
	public int insert(Object arg0) throws BusinessException {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void editSEO(SEODTO seoDTO) throws BusinessException {
		SEO seo = seoReadMapper.selectByPrimaryKey(seoDTO.getId());
		setSEOBySEODTO(seo,seoDTO);
		seo.setLastModifiedBy(seoDTO.getUserId());
		seo.setLastModifiedDtm(new Date());
		seoWriteMapper.updateByPrimaryKeySelective(seo);
	}

	@Override
	public void deleteSEO(Integer id) throws BusinessException {
		SEO seo = new SEO(); 
		seo.setId(id);
		//activityWriteMapper.deleteByPrimaryKey(id);
		seo.setLastModifiedBy(1);//假设当前操作管理员id为1
		seo.setLastModifiedDtm(new Date());
		seo.setDeleted(true);
		seoWriteMapper.updateByPrimaryKeySelective(seo);
	}

	@Override
	public String getValueByCode(String code) throws BusinessException {
		return seoReadMapper.getValueByCode(code);
	}

	

}
